package com.outsider.music.controller;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.outsider.interceptor.ResponseHeaderInterceptor;
import com.outsider.music.dao.Playlist;
import com.outsider.music.dao.User;
import com.outsider.music.service.MusicService;
import com.outsider.util.music.BaiduMusic;
import com.outsider.util.music.Music;
import com.outsider.util.music.Netease;
import com.outsider.util.music.XiamiMusic;

@Before(ResponseHeaderInterceptor.class)
public class MusicController extends Controller{
	private MusicService musicService=new MusicService();
    public void search(){
    	Integer source=getParaToInt("source");
    	String keyword=getPara("keywords");
    	List<Music> resutl=null;
    	if (source==0){
    		resutl=Netease.search(keyword);
    	}
    	else if (source==1){
    		resutl=XiamiMusic.search(keyword);
    	}
    	else if (source==2){
    		resutl=BaiduMusic.search(keyword);
    	}
    	renderJson("result", resutl);
    }
	public void getUrl() {
		String source=getPara("source");
		String sourceID=getPara("ID");
		String singer=getPara("singer");
		String name=getPara("name");
		String album=getPara("album");
		Integer playtime=getParaToInt("playtime");
		if (sourceID.split("_").length==2)
			sourceID=sourceID.split("_")[1];
		com.outsider.music.dao.Music music=new com.outsider.music.dao.Music();
		music.setName(name);
		music.setSinger(singer);
		music.setAlbum(album);
		music.setSource(source);
		music.setSourceID(sourceID);
		music.setPlaytime(playtime);
		setAttr("url",musicService.getUrl(music));
		setAttr("id",source+"_"+sourceID);
		if (getPara("index")!=null){
			setAttr("index", getPara("index"));
		}
		renderJson();
	}
	public void singer(){
		String singerID=getPara("singerID");
		String source=getPara("source");
		if (source.equals(Music.SOURCE_NETEASE))
		renderJson(Netease.getListBySinger(singerID));
		else if (source.equals(Music.SOURCE_XIAMI))
		renderJson(XiamiMusic.getListBySinger(singerID));
	}
	public void album(){
		String albumID=getPara("albumID");
		String source=getPara("source");
		if (source.equals(Music.SOURCE_NETEASE))
		renderJson(Netease.getListByAlbum(albumID));
		else if (source.equals(Music.SOURCE_XIAMI))
		renderJson(XiamiMusic.getListByAlbum(albumID));
	}
    public void createPlaylist(){
    	//list info
    	String title=getPara("listTitle");
    	Integer userID=getParaToInt("userID");
    	//music info
		String source=getPara("source");
		String sourceID=getPara("id");
		String singer=getPara("singer");
		String singerID=getPara("singerID");
		String name=getPara("title");
		String album=getPara("album");
		String albumID=getPara("albumID");
		
		Playlist playlist=new Playlist();
		playlist.setTitle(title);
		playlist.setUserID(userID);
		Music music=new Music();
		music.setName(name);
		music.setSource(source);
		music.setID(sourceID);
		music.setAlbum(album);
		music.setAlbumID(albumID);
		music.setSinger(singer);
		music.setSingerID(singerID);
		musicService.createPlaylist(playlist, music);
		renderNull();
		
    }
    public void showPlaylist(){
    	Integer userID=getParaToInt("userID");
    	setAttr("result", Playlist.dao.findPlayList(userID));
    	renderJson();
    }
    public void playlistDetail(){
    	Integer id=getParaToInt("ID");
    	Playlist playlist=Playlist.dao.findById(id);
    	JSONObject jsonObject=new JSONObject();
    	jsonObject.put("title", playlist.getTitle());
    	jsonObject.put("list_id", playlist.getID());
    	jsonObject.put("is_mine", 1);
    	
    	String json=new String(playlist.getJson());
    	jsonObject.put("tracks", JSONArray.parse(json));
    	renderJson(jsonObject.toJSONString());
    }
    public void addPlaylist(){
    	//list info
    	Integer listID=getParaToInt("listID");
    	Playlist playlist=Playlist.dao.findById(listID);
    	//music info
    	String name=getPara("title");
		String sourceID=getPara("id");
		String singer=getPara("singer");
		String singerID=getPara("singerID");
		String album=getPara("album");
		String albumID=getPara("albumID");
		String source=getPara("source");
		
		Music music=new Music();
		music.setName(name);
		music.setSource(source);
		music.setID(sourceID);
		music.setAlbum(album);
		music.setAlbumID(albumID);
		music.setSinger(singer);
		music.setSingerID(singerID);
		
		musicService.addPlaylist(playlist,music);
		renderNull();
    }
    public void removePlaylist(){
    	Integer listID=getParaToInt("listID");
    	String songID=getPara("songID");
    	Playlist playlist=Playlist.dao.findById(listID);
    	musicService.removePlaylist(playlist,songID);
    	renderNull();
    	
    }
    public void deletePlaylist(){
    	Integer listID=getParaToInt("listID");
    	Playlist.dao.deleteById(listID);
    	renderNull();
    }
    
    public void login(){
    	String name=getPara("username");
    	String password=getPara("password");
    	Integer userID=User.dao.findUserID(name, password);
    	if (userID!=null){
    		setAttr("flag", "success");
    		setAttr("userID", userID);
    	}
    	else
    		setAttr("flag", "fail");
    	renderJson();
    }
    
}
